<script setup>
import { ref } from "vue";
import { resetPasswordService } from "@/api/student.js";
import { ElMessage } from "element-plus";
import { Lock } from "@element-plus/icons-vue";
import useUserStore from "@/stores/userStore.js";
const userStore = useUserStore();
const passwordData = ref({
  password: "",
  rePassword: "",
});
const checkRePassword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请再次确认密码"));
  } else if (value !== passwordData.value.password) {
    callback(new Error("请确认两次输入的密码一样"));
  } else {
    callback();
  }
};
const rules = {
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 1, max: 16, message: "长度为1~16非空字符", trigger: "blur" },
  ],
  rePassword: [{ validator: checkRePassword, trigger: "blur" }],
};
const FormRef = ref();
const resetPassword = () => {
  FormRef.value.validate(async (valid) => {
    if (valid) {
      let password = passwordData.value.password;
      let result = await resetPasswordService(
        userStore.info.studentId,
        password,
      );
      ElMessage.success(result.message ? result.message : "修改成功");
    } else {
      ElMessage.error("请检查输入信息");
    }
  });
};
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>重置密码</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form
          ref="FormRef"
          :model="passwordData"
          :rules="rules"
          label-width="100px"
          size="large"
        >
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入密码"
              v-model="passwordData.password"
            ></el-input>
          </el-form-item>
          <el-form-item prop="rePassword">
            <el-input
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入再次密码"
              v-model="passwordData.rePassword"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="resetPassword"
              >提交修改</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>
